package com.zyicoding.myfactory.controller;

import com.zyicoding.myfactory.pojo.Factoryorder;
import com.zyicoding.myfactory.pojo.Productionline;
import com.zyicoding.myfactory.pojo.TemPojo;
import com.zyicoding.myfactory.service.*;
import com.zyicoding.myfactory.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
public class FactoryController {
    @Autowired
    private FactoryLightService factoryLightService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private ProductionLineService productionLineService;

    @Autowired
    private CarInfoService carInfoService;

    @Autowired
    private FactoryMoneyService factoryMoneyService;

    @Autowired
    private GoodsInfoService goodsInfoService;

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private CarConfService carConfService;

    @Autowired
    private FactoryOrderService factoryOrderService;

    @Autowired
    private JobService jobService;

    @Autowired
    private WorkerService workerService;

    /**
     * 获取灯光状态
     *
     * @return
     */
    @PostMapping("getLightStatus")
    public JsonResult getLightStatus() {
        return JsonResult.ok(factoryLightService.selectLightStatus().getLightStatus());
    }

    /**
     * 获取所有员工岗位
     *
     * @return
     */
    @PostMapping("selectAllJob")
    public JsonResult selectAllJob() {
        return JsonResult.ok(jobService.selectAllJob());
    }

    /**
     * 根据id查询岗位
     *
     * @return
     */
    @PostMapping("selectJobById")
    public JsonResult selectJobById(@RequestParam Integer id) {
        return JsonResult.ok(jobService.selectJobById(id));
    }

    /**
     * 改变灯光状态
     *
     * @param lightStatus 开灯、关灯
     * @return
     */
    @PostMapping("changeLightStatus")
    public JsonResult changeLightStatus(@RequestParam String lightStatus) {
        if (!lightStatus.equals("开灯") && !lightStatus.equals("关灯")) {
            return JsonResult.error("参数错误!");
        }
        return JsonResult.ok(factoryLightService.changeLightStatus(lightStatus));
    }

    /**
     * 查询所有员工
     *
     * @return
     */
    @PostMapping("selectAllEmployee")
    public JsonResult selectAllEmployee() {
        return JsonResult.ok(employeeService.selectAllEmployee());
    }

    /**
     * 获取工厂温度与环境温度
     *
     * @return
     */
    @PostMapping("getTemList")
    public JsonResult getTemList() {
        ArrayList<Integer> factoryTem = new ArrayList<>();
        ArrayList<Integer> environmentTem = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            Integer tem1 = new Integer((int) (Math.random() * 23) - 15);
            factoryTem.add(tem1);

            Integer tem2 = new Integer((int) (Math.random() * 28) - 10);
            environmentTem.add(tem2);
        }
        TemPojo temPojo = new TemPojo();
        temPojo.setEnvironmentTem(environmentTem);
        temPojo.setFactoryTem(factoryTem);

        return JsonResult.ok(temPojo);
    }

    /**
     * 根据id删除员工
     *
     * @return
     */
    @PostMapping("deleteEmployeeById")
    public JsonResult deleteEmployeeById(@RequestParam Integer id) {
        boolean result = employeeService.deleteEmployeeById(id);
        if (result)
            return JsonResult.ok("删除成功！");
        return JsonResult.error("该用户不存在或已被删除！");
    }

    /**
     * 查询所有生产线
     *
     * @return
     */
    @PostMapping("selectAllProductionLine")
    public JsonResult selectAllProductionLine() {
        return JsonResult.ok(productionLineService.selectAllProductionLine());
    }

    /**
     * 查询所有车辆信息
     *
     * @return
     */
    @PostMapping("selectAllCarInfo")
    public JsonResult selectAllCarInfo() {
        return JsonResult.ok(carInfoService.selectAllCarInfo());
    }


    /**
     * 根据id查询车辆信息
     *
     * @return
     */
    @PostMapping("selectCarById")
    public JsonResult selectCarById(@RequestParam Integer id) {
        return JsonResult.ok(carInfoService.selectCarById(id));
    }

    /**
     * 查询工厂资金
     *
     * @return
     */
    @PostMapping("selectFactoryMoney")
    public JsonResult selectFactoryMoney() {
        return JsonResult.ok(factoryMoneyService.selectFactoryMoney());
    }

    /**
     * 查询所有商品数据
     *
     * @return
     */
    @PostMapping("selectAllGoodsInfo")
    public JsonResult selectAllGoodsInfo() {
        return JsonResult.ok(goodsInfoService.selectAllGoodsInfo());
    }

    /**
     * 查询所有供应商
     *
     * @return
     */
    @PostMapping("selectAllSupplier")
    public JsonResult selectAllSupplier() {
        return JsonResult.ok(supplierService.selectAllSupplier());
    }

    /**
     * 根据id查询供货商
     *
     * @return
     */
    @PostMapping("selectSupplierById")
    public JsonResult selectSupplierById(@RequestParam Integer id) {
        return JsonResult.ok(supplierService.selectSupplierById(id));
    }

    /**
     * 购买商品
     *
     * @param id    商品id
     * @param count 商品数量
     * @return
     */
    @PostMapping("buyGoods")
    public JsonResult buyGoods(@RequestParam Integer id, @RequestParam Integer count) {
        return goodsInfoService.buyGoods(id, count);
    }

    /**
     * 改变生产线状态
     *
     * @param productionline
     * @return
     */
    @PostMapping("changeLineStatus")
    public JsonResult changeLineStatus(@RequestBody Productionline productionline) {
        if (productionline.getId() == null || productionline.getStatus() == null) {
            return JsonResult.error("请求缺少参数！");
        }

        if (!productionline.getStatus().equals("启动") && !productionline.getStatus().equals("关闭")) {
            return JsonResult.error("参数错误!");
        }

        return JsonResult.ok(productionLineService.changeLineStatus(productionline));
    }

    /**
     * 查询所有车辆配置
     *
     * @return
     */
    @PostMapping("selectAllCarConf")
    public JsonResult selectAllCarConf() {
        return JsonResult.ok(carConfService.selectAllCarConf());
    }

    /**
     * 按id查询车辆配置
     *
     * @param
     * @return
     */
    @PostMapping("selectCarconfById")
    public JsonResult selectCarconfById(@RequestParam Integer id) {
        return JsonResult.ok(carConfService.selectCarconfById(id));
    }

    /**
     * 提交车辆订单
     *
     * @param
     * @return
     */
    @PostMapping("insertOrder")
    public JsonResult insertOrder(@RequestBody Factoryorder factoryorder) {
        if (factoryorder.getCarconfid() == null || factoryorder.getCarid() == null)
            return JsonResult.error("请检查参数完整！");
        return JsonResult.ok(factoryOrderService.insertOrder(factoryorder));
    }

    /**
     * 查询所有订单
     *
     * @param
     * @return
     */
    @PostMapping("selectAllOrder")
    public JsonResult selectAllOrder() {
        return JsonResult.ok(factoryOrderService.selectAllOrder());
    }


    /**
     * 按id查询生产线
     *
     * @param
     * @return
     */
    @PostMapping("selectLineById")
    public JsonResult selectLineById(@RequestParam Integer id) {
        return JsonResult.ok(productionLineService.selectLineById(id));
    }

    /**
     * 查询所有工人
     *
     * @param
     * @return
     */
    @PostMapping("selectAllWorker")
    public JsonResult selectAllWorker() {
        return JsonResult.ok(workerService.selectAllWorker());
    }

    /**
     * 按id查询工人
     *
     * @param
     * @return
     */
    @PostMapping("selectWorkerById")
    public JsonResult selectWorkerById(@RequestParam Integer id) {
        return JsonResult.ok(workerService.selectWorkerById(id));
    }
}
